package 力扣日常刷题.木22二月.第02天207;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/2/7
 * @description
 */
public class Demo21链表中倒数第k个节点双指针 {


    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        // 先让fast走k步  两者相距k步
        while(fast!=null && k > 0){
            k--;
            fast = fast.next;
        }
        while(fast!= null) {
            fast =fast.next;
            slow = slow.next;
        }
        return slow;
    }


}
